//
//  TemplateView.m
//  ResourceLoader
//
//  Created by winston on 13-4-10.
//  Copyright (c) 2013年 winston. All rights reserved.
//

#import "TemplateView.h"

@interface TemplateView (PrivateMethod)

-(void)loadTemplate:(NSDictionary*)data;

@end

@implementation TemplateView

@synthesize smallImageName;
@synthesize middleImageName;
@synthesize bigImageName;
@synthesize textString;
@synthesize contentString;
@synthesize videoName;
//@synthesize autoID;
//@synthesize moduleKey;
@synthesize rootPath;
@synthesize delegate;
@synthesize type;
@synthesize playPath;

-(id)initWithTemplate:(NSDictionary*)data
{
    
    self = [super initWithFrame:CGRectMake(0, 0, TemplateWidth, TemplateHeight)];
    if (self)
    {
//        [self setBackgroundColor:[UIColor redColor]];
        self.clipsToBounds = YES;
        self.autoresizesSubviews = YES;
        
        backimageViewSmall = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, TemplateWidth, TemplateHeight)];
        [self addSubview:backimageViewSmall];
        [backimageViewSmall release];
        backimageViewSmall.image = [UIImage imageNamed:@"AllAuto_Appearance_Image_Small"];
        
        backimageViewMiddle = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, TemplateWidth, TemplateHeight)];
        [self addSubview:backimageViewMiddle];
        [backimageViewMiddle release];
        backimageViewMiddle.image = [UIImage imageNamed:@"AllAuto_Appearance_Image_Middel"];
        backimageViewMiddle.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleTopMargin;
        backimageViewMiddle.hidden = YES;
        
        [self loadTemplate:data];
        
        UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleFingerEvent:)];
        singleFingerOne.numberOfTouchesRequired = 1;
        singleFingerOne.numberOfTapsRequired = 1;
        [self addGestureRecognizer:singleFingerOne];
        [singleFingerOne release];
    }
    return self;
}

-(void)loadTemplate:(NSDictionary*)data
{
    self.smallImageName = [data objectForKey:@"smallimage"];
    self.middleImageName = [data objectForKey:@"middleimage"];
    self.bigImageName = [data objectForKey:@"bigimage"];
    self.textString = [data objectForKey:@"text"];
    NSString *trimmedString = [[data objectForKey:@"content"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if (![trimmedString isEqualToString:@""]) {
        self.contentString = [data objectForKey:@"content"];
    }
    
    self.videoName = [data objectForKey:@"video"];
}

-(void)renderTemplate
{
    //virtual method
}

-(void)TransmutationSmall
{
    //virtual method
}

-(void)TransmutationMiddle
{
    //virtual method
}

- (void)handleSingleFingerEvent:(UITapGestureRecognizer *)sender
{
    if (sender.numberOfTapsRequired == 1) {
        [delegate ViewBeTouch:self];
    }
}

-(CGFloat)caculateTitleHeight:(NSString*)title FontSize:(int)size Width:(int)width
{
    
    if (title == nil ||[title isEqualToString:@""]) {
        return 0;
    }
    CGSize labelSize = [title sizeWithFont:[UIFont fontWithName:@"Arial-BoldMT" size:size] constrainedToSize:CGSizeMake(width, 1000) lineBreakMode:NSLineBreakByWordWrapping];
    
    float height = labelSize.height;
    
    return height;
}

//-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
//{
//    UIView * view = [super hitTest:point withEvent:event];
//    if (view == self) {
//        return nil;
//    }
//    
//    return view;
//}

-(void)dealloc
{
    self.smallImageName = nil;
    self.middleImageName = nil;
    self.bigImageName = nil;
    self.textString = nil;
    self.videoName = nil;
//    self.moduleKey = nil;
//    self.autoID = nil;
    self.rootPath = nil;
    self.playPath = nil;
    [super dealloc];
}


@end
